package com.localization;
import java.util.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;

public class Localization
{
	
	
	private boolean Debug = true;
	
	String path;
	Hashtable<String, String> strTable;
	
	//singleton
	private static Localization instance;
	
	public static Localization getInstance()
	{
		if(instance == null)
			instance = new Localization();
		
		return instance;
	}
	
	public static String OS = System.getProperty("os.name").toLowerCase();
	
	public boolean isWindownsOS(){
		
		return   (OS.indexOf("win") >= 0) ? true : false ;
		 
	}
	
	
	public boolean isMacOS(){
		
		return   (OS.indexOf("mac") >= 0) ? true : false ;
		 
	}
	
	private Localization( ){
		strTable = new  Hashtable<String, String>();
		//this.path = getClass().getProtectionDomain().getCodeSource().getLocation().toString().substring(6);
		this.path = getClass().getProtectionDomain().getCodeSource().getLocation().toString();//.substring(5);
		
		
		if(Config.Debug)
			System.out.println("1#path#:"+this.path);
		
		//path
		if(isMacOS()){
			this.path =	this.path.replaceFirst("file:", "");
			
			if(Config.Debug)
				System.out.println("MAC");
		}
		else if(isWindownsOS()){
			
			if(Config.Debug)
				System.out.println("WIN");
				this.path.replaceFirst("file://","/" );
		}
		
		System.out.println( System.getProperty("os.name").toLowerCase());
		
		 if(Config.Debug)
			System.out.println("path#" +this.path);
	}
	
	public String getText(String key){
	
		System.out.println("key"+key+" value:"+strTable.get(key));
		return	strTable.get(key);
	}
	
	
	public void loadResource(String file){
		try{  
			File f = new File( path + file);
			FileReader fr = new FileReader(f);  
			BufferedReader br = new BufferedReader(fr);   
			String eachLine;
			String result[] = null;
			
			while( (eachLine= br.readLine())!=null){  
				
				result = eachLine.split("=");
				
				if(result[0] != null){ //key
					strTable.put(result[0],result[1]);
					//System.out.println("result[0]"+result[0]);
					//System.out.println("result[1]"+result[1]);
				}
				
			}   
		}catch( Exception e ){ 
				
			e.printStackTrace();	
		}	
	}
	
	/*public static void main(String[] Args)
	{
			Localization loc = Localization.getInstance();
			
			loc.loadResource("localized_en.txt");
			loc.getText("MENU_CONS");
			loc.getText("MENU_PROS");
			loc.getText("MENU_TITLE");
			loc.getText("MENU_DESCRIPTION");
	}
	
	*/
	
}